<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>index.html</title>
</head>
<body>
<h1>index.html</h1>
<a href="" id="login" style="display: none">LOGIN</a>
<a href="" id="register" style="display: none">REGISTER</a>
<a href="" th:if="${loginUser}!=null" th:text="${loginUser.username}"></a>
<input id="token" th:value="${token}" type="hidden">
<a href="javascript:0;" id="test">test</a>
</body>
<script src="/jquery-3.5.1.js" type="text/javascript"></script>
<script>
    let tokenVal = $("#token").val()
    let $login = $("#login");
    let $register = $("#register");
    if (tokenVal !== null && tokenVal !== '') {
        window.localStorage.setItem("token", tokenVal)
    }
    let token = localStorage.getItem("token");
    if (token == null || token === '') {
        $login.show();
        $register.show();
    }
    let thisHttpUrl = window.btoa(window.location.href)
    $login.attr("href", 'http://127.0.0.1:8888/loginPage/' + thisHttpUrl)
    $register.attr("href", 'http://127.0.0.1:8888/register/' + thisHttpUrl)

    $("#test").click(() => {
        $.ajax({
            url: "/test",
            beforeSend: function(request) {
                request.setRequestHeader("Authorization", token);
            },
            success: function (res) {
                let result = JSON.parse(res);
                if (result.status === 401){
                    location.href = 'http://127.0.0.1:8888/loginPage/' + thisHttpUrl
                }
                if (result.status === 200){
                    alert("请求成功"+result.message)
                }
            }
        });
    })
</script>
</html>

